package 中等;

public class 整数转罗马数字 {

    public static String intToRoman(int num) {
        int divisor  = 1000;
        int times = 3;
        String[][] romans = {{"I","V"},{"X","L"},{"C","D"},{"M",""}};
        StringBuffer sb = new StringBuffer();
        while (divisor != 0){
            int i = num % divisor;
            int j = num / divisor;
            if (j != 0){
                if (j < 4){
                    for (int m = 0; m < j; m++) {
                        sb.append(romans[times][0]);
                    }
                }else if (j == 4){
                    sb.append(romans[times][0]);
                    sb.append(romans[times][1]);
                }else if (j < 9){
                    sb.append(romans[times][1]);
                    for (int m = 5; m < j; m++) {
                        sb.append(romans[times][0]);
                    }
                }else {
                    sb.append(romans[times][0]);
                    sb.append(romans[times+1][0]);
                }

            }
            num = i;
            divisor /= 10;
            times--;
        }
        return sb.toString();
    }

    public static void main(String[] args) {
        System.out.println(intToRoman(58));
    }
}
